<?php
namespace Hive\Ressource;
use Fox\Common\Localization\SubRegion;

use Fox\Common\Localization\Grouping;

use Fox\Common\Localization\Region;

use Hive\Ressource\Wish\Localization;

use Doctrine\DBAL\Connection;
use Hive\Ressource;
/**
 * Wish Object.
 *
 * To store what are the wishes of the ressource.
 *
 * @author Xavier VILAIN
 * @package ressource
 * @property int 	$did 				Interview Id
 * @property int 	$ressource_id		Id of the interviewed ressource
 * @property text 	$joblist			List of look for jobs
 * @property float 	$salary				Salary
 * @property float 	$bonus				Bonus
 * @property string $localisations		Localisations (list of place to work)
 * @property string $company_size_type	Type of company (small,big...)
 * @property string $mood				Acutal Mood (want to change, waiting for a goog oportunity...)
 * @property bool 	$loekz 				Deletion Flag if true is an been deleted
 */
class Wish extends \Fox\Object {

	/**
	 * Link to the ressource
	 *
	 * @var string
	 */
	const LINK_RESSOURCE = 'ressource';
	
	/**
	 * Link to the localizations
	 *
	 * @var string
	 */
	const LINK_LOCALIZATIONS = 'localizations';
	
	/**
	 * Link to mood object
	 * @var string
	 */
	const LINK_MOOD = 'mood';
	
	/**
	 * Link to company type object
	 * @var string
	 */
	const LINK_COMPANY_TYPE = 'companyType';
			
	/**
	 *
	 * Return Object Content for Search Engine
	 */
	public function content()
	{
		$data = parent::getNonProtectedData();
		// Removing Useless information
		unset($data['localisations']);
		unset($data['cdate']);
		unset($data['current']);
		unset($data['regions']);
		unset($data['region_groups']);
		unset($data['region_subs']);
		return implode(',',$data);
	}
	/**
	 * (non-PHPdoc)
	 * @see Fox.Object::beforeCreate()
	 */
	protected function beforeCreate() {
		$this->cdate = $this->udate = self::sqlDateTime(time());
		$this->cname = $this->uname = \User::current()->getId();
		$this->targeted_total_salary = $this->targeted_salary + $this->targeted_bonus;
	}
	
	/**
	 * (non-PHPdoc)
	 * @see includes/classes/Fox/Object/Versioned::afterVersionCreate()
	 */
	protected function afterCreate()
	{
		// Add to the search engine
		$searchEngine = new \Fox\SearchEngine();
		$searchEngine->add($this,$this->ressource_id,Ressource::classname());
	}
	
	/**
	 * (non-PHPdoc)
	 * @see Fox.Object::beforeUpdate($changed)
	 */
	protected function beforeUpdate($changed) {
		if($changed) {
			$this->udate = self::sqlDateTime(time());
			$this->uname = \User::current()->getId();
			$this->targeted_total_salary = $this->targeted_salary + $this->targeted_bonus;
		}
		return $changed;
	}
	
	/**
	 * (non-PHPdoc)
	 * @see includes/classes/Fox/Object/Versioned::afterVersionUpdate()
	 */
	protected function afterUpdate()
	{
		// Add to the search engine
		$searchEngine = new \Fox\SearchEngine();
		$searchEngine->synchronize($this,$this->ressource_id,Ressource::classname());
	}

	/**
	 * (non-PHPdoc)
	 * @see Fox.Object::delete()
	 */
	public function delete() {
		parent::delete();
		$searchEngine = new \Fox\SearchEngine();
		$searchEngine->delete($this,$this->ressource_id,Ressource::classname());
	}
	
	/**
	 *
	 * @return localisation array (to initializa the localization tree)
	 */
	public function localisations()
	{
		$locArray = array();
		$localisations = $this->localisations;
		if(!empty($localisations))
		{
			$locArray = json_decode($localisations,true);
		}
		if(!is_array($locArray))
		{
			return array();
		}
		return $locArray;
	}

	/**
	 * @return float Salary + Bonus
	 */
	public function fullSalary()
	{
		return $this->targeted_salary + $this->targeted_bonus;
	}
	
	/**
	 * 
	 * Enter description here ...
	 */
	public function formatLocalizationTokens() {
		foreach($this->localizations() as $localization)  {
			switch ($localization->level) {
				case Localization::LEVEL_GROUP :
					$name = \Fox\Dao::load(Grouping::className(),$localization->id)->name;
					$tokens[]= array('id' => $localization->code,'name' => $name);
					break;
				case Localization::LEVEL_REGION :
					$name = \Fox\Dao::load(Region::className(),$localization->id)->name;
					$tokens[]= array('id' => $localization->code,'name' => $name);
					break;
				case Localization::LEVEL_SUBREGION:
					$name = \Fox\Dao::load(SubRegion::className(),$localization->id)->name;
					$tokens[]= array('id' => $localization->code,'name' => $name);
					break;	
			}
		}
		
		if(isset($tokens)) {
			return preg_replace("/\"/","'",\Fox\Utilities::jsonUTF8Encode($tokens));
		}
		return null;
	}

}
\Fox\Dao::infos(
	Wish::className(),
	new \Fox\Object\Info('ressource_desires', \Fox\Databases::DB_APP)
)->addAutoIncrementField('did');

// Link To Wishes Localization
\Fox\Object\Links::add(\Fox\Dao::link(Wish\Localization::className(),Wish\Localization::LINK_WISH)
	->to(Wish::className(), Wish::LINK_LOCALIZATIONS)
	->on('did','ressource_id')
	->versionFrom()
);

// Link To Wishes Mood
\Fox\Object\Links::add(\Fox\Dao::link(Wish::className(),Wish::LINK_MOOD)
	->to(\Hive\Contact\Wish\Mood::className(),\Hive\Contact\Wish\Mood::LINK_WISHES)
	->on('mood')
	->oneToOne()
);

// Link To Wishes Mood
\Fox\Object\Links::add(\Fox\Dao::link(\Hive\Contact\Wish\Company\Size::className(),\Hive\Contact\Wish\Mood::LINK_WISHES)
	->to(Wish::className(),Wish::LINK_COMPANY_TYPE)
	->on('company_size_type')
	->oneToOne()
);